/*
  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.

  Copyright (c) 2009 PRO ACTIVE SAS - www.proactive-group.com
   
  Ref_Find.c
  -------------

  This is the reference applications that shows how to poll the reader,
  showing each incoming tag identifier
  
  LTX 25/08/2009 : initial release

*/
#include "products/springprox/springprox.h"

#ifdef WIN32
#include <conio.h>              /* For kbhit and getch */
#endif
#ifdef __linux
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
  SWORD rc;
  int i;
  char s_buffer[64];
  BYTE uid[12];
  BYTE uid_len = 12;
  DWORD features;
  WORD proto = 0;

  char *device = NULL;

  if (argc > 1)
    device = argv[1];

  printf("SpringCard SDK for K531/SpringProx-CF/CSB Legacy\n");
  printf("\n");
  printf("WAIT : multi-protocol card polling reference demo\n");
  printf("-------------------------------------------------\n");
  printf("www.springcard.com\n\n");

  /* Open reader */
  /* ----------- */

  rc = SPROX_GetLibrary(s_buffer, sizeof(s_buffer));
  if (rc != MI_OK)
  {
    printf("Failed to get API version\n");
    goto done;
  }
  printf("API version : %s\n", s_buffer);
  printf("\n");

  /* Open reader */
  /* ----------- */

  rc = SPROX_ReaderOpen(device);
  if (rc != MI_OK)
  {
    printf("Reader not found\n");
    goto done;
  }

  printf("Reader found\n");
  rc = SPROX_ReaderGetDevice(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader found on %s\n", s_buffer);

  rc = SPROX_ReaderGetFirmware(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader firwmare is %s\n", s_buffer);

  rc = SPROX_ReaderGetFeatures(&features);
  if ((rc == MI_OK) && !(features & 0x00000002))
  {
    printf("Warning: this reader is not 15693-enabled\n");
  }

  printf("\n------------------------------\n");


  printf("Waiting for tags... (Press <Ctrl>+C to exit)\n\n");

  uid_len = sizeof(uid);

  rc = SPROX_FindWait(PROTO_ANY, &proto, uid, &uid_len, 0xFFFF, 100); 
	if (rc != MI_OK)
	{
		printf("\n[Failed]\n");
  } else
  {
		printf("\nTag found [OK]\n");
		printf("Family=");

		switch (proto)
		{
		  case PROTO_14443_A	   : printf("ISO/IEC 14443-A (or NXP Mifare)\n"); break;
		  case PROTO_14443_B	   : printf("ISO/IEC 14443-B\n"); break;
		  case PROTO_15693_STD	 :
		  case PROTO_15693_FAST	 : printf("ISO/IEC 15693\n"); break;
		  case PROTO_ICODE1_STD	 :
		  case PROTO_ICODE1_FAST : printf("NXP ICODE-1\n"); break;
		  case PROTO_MEM_PICO	   : printf("Inside Contactless PicoTAG\n"); break;
		  case PROTO_MEM_ST		   : printf("S.T. MicroElectronics SR\n"); break;
		  case PROTO_MEM_ASK	   : printf("ASK CTS256B/CTS512B\n"); break;
		  case PROTO_14443_Bi	   : printf("Calypso (Innovatron protocol)\n"); break;
		  case PROTO_EPC		     : printf("EPC\n"); break;
		  default				         : printf("%04X\n", proto);
		}
    
		printf("UID=");
		for (i=0; i<uid_len; i++)
		  printf("%02X", uid[i]);
		printf("\n");
	}


  /* Close the reader */
  SPROX_ControlRF(FALSE);
  SPROX_ReaderClose();

done:
  /* Display last error */
  if (rc == MI_OK)
  {
    printf("Done\n");
  } else
  {
    printf("%s (%d)\n", SPROX_GetErrorMessage(rc), rc);
  }
  return 0;
}
